package com.example.batbat.view;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.SearchView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.batbat.R;
import com.example.batbat.databinding.ActivityMainBinding;
import com.example.batbat.viewmodel.MainViewModel;
import com.example.common.component.MyToast;
import com.example.common.search.SearchActivity;
import com.example.common.user.GlobalUser;
import com.example.common.user.view.LoginMainActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;

@Route(path = "/app/mainActivity")
public class MainActivity2 extends AppCompatActivity {
    private String TAG = "MainActivity2";
    private Toolbar toolbar;
    private ActionBarDrawerToggle drawerToggle;//抽屉的开启和关闭的回调
    private DrawerLayout drawerLayout;//抽屉控件
    private MainViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding=DataBindingUtil.setContentView(this,R.layout.activity_main);
        binding.setGlobalUser(GlobalUser.getInstance());
        //获取navigation
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
        //获取Fragment
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
        //navController管理页面的导航
        NavController navController = navHostFragment.getNavController();
        //页面变更时navigation的UI自动更改
        NavigationUI.setupWithNavController(bottomNavigationView, navController);

        //获取ViewModel
        viewModel= new ViewModelProvider(this,new ViewModelProvider
                .AndroidViewModelFactory(getApplication()))
                .get(MainViewModel.class);

        //设置生命周期
        binding.setLifecycleOwner(this);
        //顶部导航栏toolbar
        toolbar=findViewById(R.id.app_toolbar);

        //bar头像
        ImageView img = findViewById(R.id.app_avatar);
        img.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onClick(View v) {
                if(!drawerLayout.isDrawerOpen(Gravity.START)){
                    drawerLayout.openDrawer(Gravity.START);
                    binding.setGlobalUser(GlobalUser.getInstance());
                }
            }
        });

        //退出登录
        binding.appDrawerExit.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onClick(View v) {
                GlobalUser.getInstance().deleteToken(getApplicationContext());
                binding.setGlobalUser(GlobalUser.getInstance());
                drawerLayout.closeDrawer(Gravity.START);//关闭抽屉
                MyToast.showMyToast(getApplicationContext(),"已退出登录",1);
                viewModel.getIsLogin().setValue(false);
            }
        });

        //获取抽屉控件
        drawerLayout=(DrawerLayout)findViewById(R.id.my_drawer);

        //监听登录状况
        viewModel.getIsLogin().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    binding.appAvatar.setVisibility(View.VISIBLE);
                }else {
                    binding.appAvatar.setImageResource(R.drawable.ic_baseline_account_circle_24);
                }
            }
        });

        //搜索监听绑定
        binding.appSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //文字提交的时候回调，query是最后提交搜索的文字
                Intent intent=new Intent(getApplicationContext(), SearchActivity.class);
                intent.putExtra("SEARCH",query);
                startActivity(intent);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //在文字改变的时候回调，newText是改变之后的文字
                return false;
            }
        });

    }

}